package cn.itsource.service.impl;

import cn.itsource.constants.Constants;
import cn.itsource.constants.GlobalExceptionCode;
import cn.itsource.mapper.DriverMapper;
import cn.itsource.pojo.domain.Driver;
import cn.itsource.pojo.domain.DriverSetting;
import cn.itsource.pojo.domain.DriverSummary;
import cn.itsource.pojo.domain.DriverWallet;
import cn.itsource.pojo.domain.dto.wxRegisterDto;
import cn.itsource.pojo.domain.properties.DriverSettingProperties;
import cn.itsource.remote.api.LoginApi;
import cn.itsource.remote.pojo.param.AppLoginParam;
import cn.itsource.result.R;
import cn.itsource.service.IDriverService;
import cn.itsource.service.IDriverSettingService;
import cn.itsource.service.IDriverSummaryService;
import cn.itsource.service.IDriverWalletService;
import cn.itsource.template.WeChatTemplate;
import cn.itsource.utils.AssertUtil;
import cn.itsource.utils.BitStatesUtil;
import cn.itsource.utils.NameUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.math.BigDecimal;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 司机对象 服务实现类
 * </p>
 *
 * @author Neuronet
 * @since 2024-01-28
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {
    @Autowired
    private IDriverSettingService driverSettingService;
    @Autowired
    private IDriverSummaryService driverSummaryService;
    @Autowired
    private IDriverWalletService driverWalletService;

    @Autowired
    private DriverSettingProperties driverSettingProperties;

    @Autowired
    private WeChatTemplate weChatTemplate;

    @Autowired
    private LoginApi loginApi;


    //司机微信注册
    @Override
    @Transactional//报错就回滚
    public void wxRegister(wxRegisterDto wxRegisterDto) {
        String openIdCode = wxRegisterDto.getOpenIdCode();
        String phoneCode = wxRegisterDto.getPhoneCode();
//    参数校验
//        业务校验
        String openId = weChatTemplate.getOpenId(openIdCode);

//        用户是否注册
        Driver one = super.getOne(new LambdaQueryWrapper<Driver>().eq(Driver::getOpenId, openId));
        AssertUtil.isNull(one,GlobalExceptionCode.PARAM_PHONE_EXIST);
        //业务实现
//        保存driver
      /*  String phoneNumber = weChatTemplate.getPhoneNumber(phoneCode);*/
        String phoneNumber="13693474073";
        Long id = saveDriver(openId,phoneNumber);
//        初始化t_driver_setting
        initdriversetting(id);
        initdriversummary(id);
        initdriverwallet(id);
//        保存login
        AppLoginParam appLoginParam = new AppLoginParam();
        appLoginParam.setId(id);
        appLoginParam.setType(Constants.Login.TYPE_DRIVER);
        appLoginParam.setAvatar(Constants.DEFAULT_Avatar);
        appLoginParam.setNickName(NameUtil.getName());
        appLoginParam.setOpenId(openId);
        appLoginParam.setPhone(phoneNumber);

        R<Void> booleanR = loginApi.appLoginInit(appLoginParam);
        AssertUtil.isTrue(booleanR.isSuccess(),GlobalExceptionCode.SERVICE_ERROR);
    }

    private void initdriverwallet(Long id) {
        DriverWallet driverWallet = new DriverWallet();
        driverWallet.setId(id);
        driverWallet.setCreateTime(new Date());
        driverWallet.setAmount(BigDecimal.ZERO);

        driverWallet.setPassword("123456");
        driverWalletService.save(driverWallet);
    }

    private void initdriversummary(Long id) {
        DriverSummary driverSummary = new DriverSummary();
        driverSummary.setId(id);
        driverSummary.setLevel(Constants.RealAuth.VERIFYING);
        driverSummary.setTotalOrders(0);
        driverSummary.setWeekOrders(0);
        driverSummary.setGoodComments(0);
        driverSummary.setAppeal(0);
        driverSummary.setTotalComplaint(0);
        driverSummary.setTodayTotalCancel(0);
        driverSummary.setDriveDuration(0);
        driverSummary.setTradeOrders(0);
        driverSummary.setTodayTradeOrders(0);
        driverSummary.setTodayComplaint(0);
        driverSummary.setTodayCancel(0);
        driverSummary.setTodayIncome(BigDecimal.ZERO);
        driverSummary.setCreateTime(new Date());
        driverSummaryService.save(driverSummary);
    }

    private void initdriversetting(Long id) {
        DriverSetting driverSetting = new DriverSetting();
        driverSetting.setId(id);
        driverSetting.setAutoAccept(false);
        driverSetting.setOrientation(false);
        driverSetting.setListenService(false);
        driverSetting.setOrderDistance(driverSettingProperties.getOrderDistance());
        driverSetting.setRangeDistance(driverSettingProperties.getRangeDistance());
        driverSetting.setCreateTime(new Date());
        driverSettingService.save(driverSetting);
    }

    private Long saveDriver(String unionid, String phoneNumber) {
        Driver driver = new Driver();
        driver.setPhone(phoneNumber);
        driver.setCreateTime(new Date());
        driver.setOpenId(unionid);
        driver.setBitState(BitStatesUtil.OP_PHONE);
        super.save(driver);
        return driver.getId();
    }
}
